import config from './config/httpConfig.js';
import $store from '../store/index.js'
import $toast from './toast.js'
// post方法
var request = (option, is_loading = true) => {
	var params = option.params;
	var token = uni.getStorageSync('token');
	var url = (config.IS_ONLINE ? config.DOMAIN : config.DEV_DOMAIN) + option.url;
	let ClientType = ''
	//#ifdef H5
	ClientType = 'wap'
	//#endif
	//#ifdef APP-PLUS
	ClientType = 'app'
	//#endif
	//#ifdef MP-WEIXIN
	ClientType = 'wxapp'
	//#endif
	if (is_loading) {
		uni.showLoading({
			title: '加载中...'
		})
	}
	uni.request({
		url: url,
		data: params,
		method: option.method || "POST",
		header: option.header || {
			"appid": config.APPID,
			"Authorization": token,
			"ClientType": ClientType
		},
		dataType: "json",
		responseType: "text",
		success: res => {
			// console.log("请求接口"+url+"的code",res)
			if (is_loading) setTimeout(() => {
				uni.hideLoading()
			}, 300)
			option.success(res.data);
			let routes = getCurrentPages()
			let curRoute = routes[routes.length - 1].route
			if (res.data.code == 1011008 || res.data.code == 401 || res.data.code == 402) {
				// $store.commit("upTipsFlag", 'show');
				// console.log("公共js呼叫弹窗")
				if( url.includes('wechat/info')){
					$toast.confirm({
						content: '请登录',
						callback: result => {
							if (result) {
								// uni.removeStorageSync('token');
								uni.clearStorageSync()
								uni.navigateTo({
									url: '/pages/publics/login'
								})
							}
						}
					})
				}
				
			} else if (res.data.code == 1016002) {
				$toast.msg(res.data.message, {
					icon: 1
				})
			}
		},
		fail: res => {
			if (is_loading) setTimeout(() => {
				uni.hideLoading()
			}, 300)
			throw new Error(res);
		},
		complete: () => {
			if (is_loading) setTimeout(() => {
				uni.hideLoading()
			}, 300)
		}
	})
}
// get方法
var get = (option, is_loading = false) => {
	var params = option.params;
	var token = uni.getStorageSync('token');
	var url = (config.IS_ONLINE ? config.DOMAIN : config.DEV_DOMAIN) + option.url;
	let ClientType = ''
	//#ifdef H5
	ClientType = 'wap'
	//#endif
	//#ifdef APP-PLUS
	ClientType = 'app'
	//#endif
	//#ifdef MP-WEIXIN
	ClientType = 'wxapp'
	//#endif
	if (is_loading) {
		uni.showLoading({
			title: '加载中...'
		})
	}
	uni.request({
		url: url,
		data: params,
		method: option.method || "get",
		header: option.header || {
			"appid": config.APPID,
			"Authorization": token,
			"ClientType": ClientType
		},
		dataType: "json",
		responseType: "text",
		success: res => {
			// console.log("请求接口"+url+"的code",res)
			if (is_loading) setTimeout(() => {
				uni.hideLoading()
			}, 300)
			option.success(res.data);
			let routes = getCurrentPages()
			let curRoute = routes[routes.length - 1].route
			if (res.data.code == 1011008 || res.data.code == 401 || res.data.code == 402) {
				if( url.includes('wechat/info')){
					$toast.confirm({
						content: '请登录',
						callback: result => {
							if (result) {
								// uni.removeStorageSync('token');
								uni.clearStorageSync()
								uni.navigateTo({
									url: '/pages/publics/login'
								})
							}
						}
					})
				}

			} else if (res.data.code == 1016002) {
				$toast.msg(res.data.message, {
					icon: 1
				})
			}
		},
		fail: res => {
			console.log("请求500")
			if (is_loading) setTimeout(() => {
				uni.hideLoading()
			}, 300)
			throw new Error(res);
		},
		complete: () => {
			if (is_loading) setTimeout(() => {
				uni.hideLoading()
			}, 300)
		}
	})
}



var login = (option) => {
	uni.login({
		success: res => {
			var params = {
				code: res.code
			}
			request({
				that: option.that,
				c: config.LOGIN_ACTION.c,
				a: config.LOGIN_ACTION.a,
				params: params,
				success: result => {
					if (result.data.code === config.SUCCESS_CODE) {
						option.that.$store.commit("upToken", result.data.data.token);
						request(option);
					} else {
						uni.showModal({
							title: "提示",
							content: result.data.msg
						});
						return false;
					}
				}
			})
		}
	})
}
//上传图片
var handlerUploadPic = (backcall, type = 1) => {
	let sourceType = ["album", "camera"];
	if (type == 2) {
		sourceType = ["album"];
	} else if (type == 3) {
		sourceType = ["camera"];
	}
	uni.chooseImage({
		count: 9,
		sizeType: ["original", "compressed"],
		sourceType: ["album", "camera"],
		success: res => {
			var tempFilePaths = res.tempFilePaths;
			console.log("上传照片", res)
			tempFilePaths.forEach((item, index) => {

				uploadfile({
					tempFilePath: item,
					success: res => {
						res.tempNameArray = tempFilePaths //记录顺序
						backcall(res);
					}
				})
			})
			// uploadfile({
			// 	tempFilePath: tempFilePaths[0],
			// 	success: res => {
			// 		backcall(res);
			// 	}
			// })
		}
	})
}
//上传图片视频
var handlerUploadVideo = (backcall) => {
	console.log('上传视频');
	uni.chooseVideo({
		compressed: true,
		// maxDuration: 120,
		// camera: 'back',
		success: result => {
			console.log("上传视频", result)
			var tempFilePath = result.tempFilePath;
			uploadfile({
				tempFilePath: tempFilePath,
				success: res => {
					backcall(res);
					console.log("打印视频封面,并且上传", result.thumbTempFilePath, res
						.data.id)
					//上传封面，需要传id
					uni.uploadFile({
						url: config.VIDEO_BG_UPLOAD_URL,
						filePath: result.thumbTempFilePath,
						header: {
							"Content-Type": "multipart/form-data"
						},
						formData: {
							fileId: res.data.id
						},
						name: "file",
						success: res_img => {
							var imgData = JSON.parse(res_img.data);
							console.log("上传封面成", imgData)

						}
					})
				}
			})

		}
	})
}

var uploadfile = (option) => {
	uni.showLoading({
		title: "上传中..."
	});
	var url = config.IS_ONLINE ? config.UPLOAD_URL : config.DEV_UPLOAD_URL;
	uni.uploadFile({
		url: url,
		filePath: option.tempFilePath,
		header: {
			"appid": config.APPID
		},
		formData: {
			// fileId:option.
		},
		name: "file",
		success: result => {
			uni.hideLoading();
			var imgData = JSON.parse(result.data);
			if (imgData.code === config.SUCCESS_CODE) {
				option.success(imgData);
			} else {
				uni.showToast({
					title: '上传失败',
					icon: 'none'
				})
				option.success(false);
			}
		}
	})
}
//上传微信文件
var handlerUploadWx = (backcall) => {
	wx.chooseMessageFile({
		count: 10,
		type: 'file',
		success(res) {
			// tempFilePath可以作为img标签的src属性显示图片
			const tempFilePath = res.tempFiles
			console.log(res)
			uploadfiles({
				tempFilePath: tempFilePath[0].path,
				success: res => {
					backcall(res);
				}
			})
		}
	})
}
//微信上传文
var uploadfiles = (option) => {
	uni.showLoading({
		title: "上传中..."
	});
	var url = config.IS_ONLINE ? config.UPLOAD_URL : config.DEV_UPLOAD_URL;
	uni.uploadFile({
		url: url,
		filePath: option.tempFilePath,
		header: {
			"appid": config.APPID,
			"Authorization": uni.getStorageSync('token'),
		},
		name: "file",
		success: result => {
			uni.hideLoading();
			var imgData = JSON.parse(result.data);
			if (imgData.code === config.SUCCESS_CODE) {
				option.success(imgData);
				uni.showToast({
					title: '上传成功',
					icon: 'none'
				})
			} else {
				uni.showToast({
					title: '上传失败',
					icon: 'none'
				})
				option.success(false);
			}
		}
	})
}
module.exports = {
	request,
	get,
	handlerUploadPic,
	handlerUploadVideo,
	handlerUploadWx
}